{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a      2\n",
       "c      4\n",
       "d    24G\n",
       "a      7\n",
       "Name: col, dtype: object"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "i = [\"a\", \"c\", \"d\", \"a\"]\n",
    "v = [2, 4, '24G', 7]\n",
    "t = pd.Series(v, index = i, name = \"col\")\n",
    "t"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 不同索引series 数组相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ohio      35000\n",
      "Texas     71000\n",
      "Oregon    16000\n",
      "Utah       5000\n",
      "dtype: int64\n",
      "California    0\n",
      "Ohio          1\n",
      "Oregon        2\n",
      "Texas         3\n",
      "dtype: int32\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "California        0.0\n",
       "Ohio          35001.0\n",
       "Oregon        16002.0\n",
       "Texas         71003.0\n",
       "Utah           5000.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}\n",
    "obj1 = pd.Series(sdata)\n",
    "print(obj1)\n",
    "states = ['California', 'Ohio', 'Oregon', 'Texas']\n",
    "obj2 = pd.Series(np.arange(4), index = states)\n",
    "print(obj2)\n",
    "\n",
    "obj2.add(obj1,fill_value=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      name age gender\n",
      "0    Alice  25      F\n",
      "1      Bob  30      M\n",
      "2  Charlie   M      M\n",
      "3    David  40      M\n"
     ]
    }
   ],
   "source": [
    "\n",
    "data = {'name': ['Alice', 'Bob', 'Charlie', 'David'],\n",
    "        'age': [25, 30, 'M', 40],\n",
    "        'gender': ['F', 'M', 'M', 'M']}\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "print(df)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "M    3\n",
       "F    1\n",
       "Name: gender, dtype: int64"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['gender'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>b</th>\n",
       "      <th>a</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>app</th>\n",
       "      <td>0.887900</td>\n",
       "      <td>0.534595</td>\n",
       "      <td>0.383286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>win</th>\n",
       "      <td>0.572070</td>\n",
       "      <td>0.108375</td>\n",
       "      <td>0.343490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mac</th>\n",
       "      <td>0.972002</td>\n",
       "      <td>0.903596</td>\n",
       "      <td>0.860683</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            b         a         c\n",
       "app  0.887900  0.534595  0.383286\n",
       "win  0.572070  0.108375  0.343490\n",
       "mac  0.972002  0.903596  0.860683"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.DataFrame(np.random.rand(3,3),index=['app','win','mac'],columns=['b','a','c'])\n",
    "df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>b</th>\n",
       "      <th>a</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.810657</td>\n",
       "      <td>0.515522</td>\n",
       "      <td>0.529153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.210859</td>\n",
       "      <td>0.397954</td>\n",
       "      <td>0.287802</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.572070</td>\n",
       "      <td>0.108375</td>\n",
       "      <td>0.343490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.729985</td>\n",
       "      <td>0.321485</td>\n",
       "      <td>0.363388</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.887900</td>\n",
       "      <td>0.534595</td>\n",
       "      <td>0.383286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.929951</td>\n",
       "      <td>0.719095</td>\n",
       "      <td>0.621984</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>0.972002</td>\n",
       "      <td>0.903596</td>\n",
       "      <td>0.860683</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              b         a         c\n",
       "count  3.000000  3.000000  3.000000\n",
       "mean   0.810657  0.515522  0.529153\n",
       "std    0.210859  0.397954  0.287802\n",
       "min    0.572070  0.108375  0.343490\n",
       "25%    0.729985  0.321485  0.363388\n",
       "50%    0.887900  0.534595  0.383286\n",
       "75%    0.929951  0.719095  0.621984\n",
       "max    0.972002  0.903596  0.860683"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  name     sex  year city\n",
      "0   张三  female  2001   北京\n",
      "1   李四  female  2001   上海\n",
      "2   王五    male  2003   广州\n",
      "3   小明    male  2002   北京\n"
     ]
    }
   ],
   "source": [
    "data = {\n",
    "    'name':['张三', '李四', '王五', '小明'],\n",
    "    'sex':['female', 'female', 'male', 'male'],\n",
    "    'year':[2001, 2001, 2003, 2002],\n",
    "    'city':['北京', '上海', '广州', '北京']\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "print(df)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  name     sex  year city\n",
      "a   张三  female  2001   北京\n",
      "b   李四  female  2001   上海\n",
      "c   王五    male  2003   广州\n",
      "a   小明    male  2002   北京\n"
     ]
    }
   ],
   "source": [
    "df3 = pd.DataFrame(data, columns = ['name', 'sex', 'year', 'city'], index = ['a', 'b', 'c', 'a'])\n",
    "df3.index.drop_duplicates()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      one  two\n",
      "four          \n",
      "2       0    1\n",
      "5       3    4\n",
      "8       6    7\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>four</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   four  one  two\n",
       "0     2    0    1\n",
       "1     5    3    4\n",
       "2     8    6    7"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4 = pd.DataFrame(np.arange(9).reshape(3,3),\n",
    "index = ['a','c','d'],columns = ['one','two','four'])\n",
    "df5 = df4.set_index('four')\n",
    "print(df5)\n",
    "df5.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   B   C\n",
      "0  3   7\n",
      "1  4   8\n",
      "2  5   9\n",
      "3  6  10\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 创建一个 DataFrame\n",
    "df = pd.DataFrame({'B': [1, 1, 2, 2], 'B': [3, 4, 5, 6], 'C': [7, 8, 9, 10]})\n",
    "\n",
    "# 按照列名的前两个字符进行分组\n",
    "grouped = df.groupby(df.columns.str[:1], axis=1)\n",
    "\n",
    "# 计算每个组的平均值\n",
    "result = grouped.mean()\n",
    "\n",
    "print(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['A', 'B', 'C'], dtype='object')"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns.str[:1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>4.5</td>\n",
       "      <td>12.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sum</th>\n",
       "      <td>36.0</td>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         C      D\n",
       "mean   4.5   12.5\n",
       "sum   36.0  100.0"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 创建一个 DataFrame\n",
    "df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],\n",
    "                   'B': ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],\n",
    "                   'C': [1, 2, 3, 4, 5, 6, 7, 8],\n",
    "                   'D': [9, 10, 11, 12, 13, 14, 15, 16]})\n",
    "\n",
    "\n",
    "\n",
    "result = df[['C','D']].agg(['mean', 'sum'])\n",
    "result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_data=pd.DataFrame([[96,92,83,94],[85,86,77,88],[69,90,91,82]],index=['a','b','c'],\n",
    "                 columns=['月考2','月考1','月考3','月考4'])\n",
    "\n",
    "df_data[df_data > 85] = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>月考2</th>\n",
       "      <th>月考1</th>\n",
       "      <th>月考3</th>\n",
       "      <th>月考4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>100</td>\n",
       "      <td>99</td>\n",
       "      <td>83</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>85</td>\n",
       "      <td>99</td>\n",
       "      <td>77</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>69</td>\n",
       "      <td>99</td>\n",
       "      <td>100</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   月考2  月考1  月考3  月考4\n",
       "a  100   99   83  100\n",
       "b   85   99   77  100\n",
       "c   69   99  100   82"
      ]
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_data.loc[:,'月考1'] = 99\n",
    "df_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>月考2</th>\n",
       "      <th>月考1</th>\n",
       "      <th>月考3</th>\n",
       "      <th>月考4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>100</td>\n",
       "      <td>99</td>\n",
       "      <td>59</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>85</td>\n",
       "      <td>99</td>\n",
       "      <td>59</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>69</td>\n",
       "      <td>99</td>\n",
       "      <td>100</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   月考2  月考1  月考3  月考4\n",
       "a  100   99   59  100\n",
       "b   85   99   59  100\n",
       "c   69   99  100   82"
      ]
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_data['月考3'].replace([56, 60 ,77,83],59,inplace=True)\n",
    "df_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "low       5\n",
      "midlle    3\n",
      "high      2\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:title={'center':'test'}, ylabel='locals'>"
      ]
     },
     "execution_count": 199,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXgAAAEICAYAAABVv+9nAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAOIElEQVR4nO3dfYxldX3H8feHZRUCVDFMKGFdRqMpRVqlWWgNaMFSeWrRFApaJSHSboy21VQlVK19wLS0JrYSactKqQQU0tSSUqAgShFQKuzyvALRwBotCEstlbUNFfj2j3vWnX2+y8xv7u5v3q9ks/fOPXN+3+Vm35w9c+ZMqgpJUn92m/QAkqQ2DLwkdcrAS1KnDLwkdcrAS1KnDLwkdcrAS1KnDLwWtCRrkhw7y32cmeTWuZpJmisGXpI6ZeC1YCW5FFgK/EuSdUnOTvILSb6W5Kkk9yQ5esb2ZyZ5OMnTSR5J8o4kPw38LfD6YR9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54/kW/x5u43O766FH8G3cDJyeZFGSKeCNwO3Da7cB7x+2uYXRqQFPz+ycvsroFhsA7xhj++uB30kSgCSHtRpMY3kQeGWS6eH56ROcZSIMfBtXAvcC9wA3AmdX1fpzuLcAu1fVt4A7GR3FG/id0/uA9ya5A3jJGNufCywG7h0u39vuF2bVTlX9L/Ae4LoktwKPA/892anml7cqkNStJHtX1brhX1UXAN+sqr+c9FzzxSN4ST37reGL3qsZ/SvswsmOM788gpekTnkEL0mdMvCS1CkDL0mdMvCS1CkDL0md+n9gzQ5s3C0vNAAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 创建一个随机的数据集\n",
    "np.random.seed(0)\n",
    "values = np.random.rand(10)\n",
    "\n",
    "# 将数据集分成 3 个离散的区间\n",
    "cuts = pd.cut(values, bins=3,labels=['low','midlle','high'])\n",
    "\n",
    "# 统计每个区间的数量\n",
    "counts = cuts.value_counts()\n",
    "print(counts)\n",
    "counts.plot(kind=\"bar\",rot=0,ylabel=\"locals\",title=\"test\")\n",
    "# # 打印每个区间中的值\n",
    "# for i, bin in enumerate(bins.categories):\n",
    "#     print(f'Bin {i}: {bin}')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[NaN, 'Youth', 'Youth', 'Middle-aged', 'Middle-aged', 'Middle-aged', 'Senior', NaN]\n",
      "Categories (3, object): ['Youth' < 'Middle-aged' < 'Senior']\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "ages = [18, 25, 30, 35, 40, 50, 60, 70]\n",
    "bins = [20, 30, 50, 65]\n",
    "\n",
    "labels = ['Youth', 'Middle-aged', 'Senior']\n",
    "\n",
    "cuts = pd.cut(ages, bins=bins, labels=labels)\n",
    "\n",
    "print(cuts)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  customer_id     name\n",
      "0          C1    Alice\n",
      "1          C2      Bob\n",
      "2          C3  Charlie\n",
      "3          C4    David\n",
      "  order_id customer_id  amount\n",
      "0       O1          C2     100\n",
      "1       O2          C4     200\n",
      "2       O3          C1     150\n",
      "3       O4          C3      50\n",
      "  customer_id     name order_id  amount\n",
      "0          C1    Alice       O3     150\n",
      "1          C2      Bob       O1     100\n",
      "2          C3  Charlie       O4      50\n",
      "3          C4    David       O2     200\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 创建两个DataFrame\n",
    "df1 = pd.DataFrame({'customer_id': ['C1', 'C2', 'C3', 'C4'],\n",
    "                    'name': ['Alice', 'Bob', 'Charlie', 'David']})\n",
    "\n",
    "df2 = pd.DataFrame({'order_id': ['O1', 'O2', 'O3', 'O4'],\n",
    "                    'customer_id': ['C2', 'C4', 'C1', 'C3'],\n",
    "                    'amount': [100, 200, 150, 50]})\n",
    "\n",
    "# 使用merge函数将两个DataFrame在列上进行连接\n",
    "result = pd.merge(df1, df2, on='customer_id')\n",
    "\n",
    "print(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    A   B   C   D\n",
      "0  A0  B0  C0  D0\n",
      "1  A1  B1  C1  D1\n",
      "2  A2  B2  C2  D2\n",
      "3  A3  B3  C3  D3\n",
      "    A   B   C   D\n",
      "0  A4  B4  C4  D4\n",
      "1  A5  B5  C5  D5\n",
      "2  A6  B6  C6  D6\n",
      "3  A7  B7  C7  D7\n",
      "    A   B   C   D\n",
      "0  A0  B0  C0  D0\n",
      "1  A1  B1  C1  D1\n",
      "2  A2  B2  C2  D2\n",
      "3  A3  B3  C3  D3\n",
      "4  A4  B4  C4  D4\n",
      "5  A5  B5  C5  D5\n",
      "6  A6  B6  C6  D6\n",
      "7  A7  B7  C7  D7\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 创建两个DataFrame\n",
    "df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],\n",
    "                    'B': ['B0', 'B1', 'B2', 'B3'],\n",
    "                    'C': ['C0', 'C1', 'C2', 'C3'],\n",
    "                    'D': ['D0', 'D1', 'D2', 'D3']})\n",
    "\n",
    "df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],\n",
    "                    'B': ['B4', 'B5', 'B6', 'B7'],\n",
    "                    'C': ['C4', 'C5', 'C6', 'C7'],\n",
    "                    'D': ['D4', 'D5', 'D6', 'D7']})\n",
    "\n",
    "print(df1)\n",
    "\n",
    "print(df2)\n",
    "# 使用concat函数将两个DataFrame在行上进行连接\n",
    "result = pd.concat([df1, df2],ignore_index=True)\n",
    "\n",
    "print(result)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
